# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=make
_autotools=yes
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.2.1
pkgrel=2
pkgdesc="GNU make utility to maintain groups of programs (mingw-w64)"
arch=('any')
url="https://www.gnu.org/software/make"
license=('GPL3')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-gettext")
makedepends=('wget')
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.bz2"
        'make-getopt.patch'
        'make-linebuf-mingw.patch')
sha256sums=('d6e262bf3601b42d2b1e4ef8310029e1dcf20083c5446b4b7aa67081fdffc589'
            '15a86d5143c9ec6337bdd69fecb7c33c36e4fd925528dc1498b0bc4fbd31b0bb'
            'cbb8c0a1bdd6e8174febce01946bd42da26dfcb73a7338c0a1df89ac6b8e157b')


prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/make-getopt.patch
  patch -p1 -i ${srcdir}/make-linebuf-mingw.patch
}

build() {
  if [[ "${_autotools}" = "yes" ]]; then
    [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
    mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
    ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --host=${MINGW_CHOST} \
      --without-libintl-prefix \
      --without-libiconv-prefix \
      ac_cv_dos_paths=yes
    make -j1
  else
    cmd /c 'build_w32.bat --without-guile gcc'
  fi
}

package() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,include}
  if [[ "${_autotools}" = "yes" ]]; then
    cd ${srcdir}/build-${MINGW_CHOST}
    cp -f make.exe ${pkgdir}${MINGW_PREFIX}/bin/mingw32-make.exe
    cp -f ${srcdir}/${_realname}-${pkgver}/gnumake.h ${pkgdir}${MINGW_PREFIX}/include/
  else
    cd ${srcdir}/${_realname}-${pkgver}
    cp -f gnumake.exe ${pkgdir}${MINGW_PREFIX}/bin/mingw32-make.exe
    cp -f libgnumake-1.dll.a ${pkgdir}${MINGW_PREFIX}/lib/
    cp -f gnumake.h ${pkgdir}${MINGW_PREFIX}/include/
  fi
}
